//
//  UIImage+hook.m
//  yuanjiang
//
//  Created by YuanJiang on 2019/9/19.
//  Copyright © 2019 llbt. All rights reserved.
//

#import "UIImage+hook.h"
#import <objc/runtime.h>

@implementation UIImage (hook)

+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class selfClass = object_getClass([self class]);
        SEL oriSEL = @selector(imageNamed:);
        Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);
        
        SEL cusSEL = @selector(myImageNamed:);
        Method cusMethod = class_getInstanceMethod(selfClass, cusSEL);
        
        BOOL addSuccess = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
        if (addSuccess) {
            class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
        }else{
            method_exchangeImplementations(oriMethod, cusMethod);
        }
    });
}

+ (UIImage *) myImageNamed:(NSString *)name
{
    NSString *string = [NSString stringWithFormat:@"%@%@",@"new_",name];
    return [self myImageNamed:string];
}

@end
